import { EggAppInfo, PowerPartial } from "egg";
const path = require('path');

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<any>;
  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1571109218048_7977";

  // add your egg config in here
  config.middleware = ["error"];

  config.logger = {
    allowDebugAtProd: true
  };

  // config.mongoose = {
  //   client: {
  //     url: "mongodb://user:usert123@localhost:27017/onerror",
  //     options: {
  //       autoReconnect: true,
  //       reconnectTries: Number.MAX_VALUE,
  //       bufferMaxEntries: 0
  //     }
  //   }
  // };

  config.validate = {
    // convert: false,
    // validateRoot: false,
  };

  config.api = {
    admin: {
      title: 'API', // Title (required)
      version: '1.0.0', // Version (required)
      description: 'Helper 项目 后台 API', // Description (optional)
      // router:'admin', //默认为key的名字，访问路径  /apidoc/admin
      // files:'/app/controller/admin/*.js' 默认控制器对应的文件夹
    }
  }
  config.multipleStatic = [
    {
      prefix: '/api/docs-admin/',
      dir: path.join(appInfo.baseDir, 'app/public/docs/admin'),
      // maxAge: 31536000,
      // buffer: false,
      // dynamic: false,
    },
    {
      prefix: '/api/docs-h5/',
      dir: path.join(appInfo.baseDir, 'app/public/docs/h5'),
      // maxAge: 31536000,
      // buffer: false,
      // dynamic: false,
    },
  ];

  config.security = {
    csrf: {
      enable: false
    },
    hsts: {
      enable: true,
      maxAge: 365 * 24 * 3600,
      includeSubdomains: true
    },
    domainWhiteList: [
      "http://0.0.0.0:8080",
      "http://0.0.0.0:8081",
      "http://0.0.0.0:8082",
      "http://0.0.0.0:8083",
      "http://10.100.13.33",
      "http://127.0.0.1:7003"
    ]
  };
  config.redis = {
    client: {
      port: 6379,          // Redis port
      host: '127.0.0.1',   // Redis host
      password: 'auth',
      db: 0,
    },
  }
  config.cors = {
    origin: (val: any) => {
      // let host = ''
      // tslint:disable-next-line:no-duplicate-case
      // switch (val.request.header.origin) {
      //   case 'http://0.0.0.0:8080': host = 'http://0.0.0.0:8080'; break;
      //   case 'http://0.0.0.0:8081': host = 'http://0.0.0.0:8081'; break;
      //   case 'http://192.168.1.121:8080': host = 'http://192.168.1.121:8080'; break;

      //   default:  break;
      // }
      // return host
      return val.request.header.origin
    },
    allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS",
    credentials: true
  };

  // XSS过滤白名单
  config.helper = {
    shtml: {
      whiteList: {
        p: ["align"],
        br: [],
        pre: [],
        code: [],
        a: ["target", "href", "title"],
        abbr: ["title"],
        address: [],
        area: ["shape", "coords", "href", "alt"],
        article: [],
        aside: [],
        audio: ["autoplay", "controls", "loop", "preload", "src"],
        b: [],
        bdi: ["dir"],
        bdo: ["dir"],
        big: [],
        blockquote: ["cite"],
        caption: [],
        center: [],
        cite: [],
        col: ["align", "valign", "span", "width"],
        colgroup: ["align", "valign", "span", "width"],
        dd: [],
        del: ["datetime"],
        details: ["open"],
        div: [],
        dl: [],
        dt: [],
        em: [],
        font: ["color", "size", "face"],
        footer: [],
        h1: [],
        h2: [],
        h3: [],
        h4: [],
        h5: [],
        h6: [],
        header: [],
        hr: [],
        i: [],
        img: ["src", "alt", "title", "width", "height", "style"],
        ins: ["datetime"],
        li: [],
        mark: [],
        nav: [],
        ol: [],
        s: [],
        section: [],
        small: [],
        span: [],
        sub: [],
        sup: [],
        strong: [],
        table: ["width", "border", "align", "valign"],
        tbody: ["align", "valign"],
        td: ["width", "rowspan", "colspan", "align", "valign"],
        tfoot: ["align", "valign"],
        th: ["width", "rowspan", "colspan", "align", "valign"],
        thead: ["align", "valign"],
        tr: ["rowspan", "align", "valign"],
        tt: [],
        u: [],
        ul: [],
        video: [
          "autoplay",
          "controls",
          "loop",
          "preload",
          "src",
          "height",
          "width"
        ]
      },
      domainWhiteList: ["cdn.web-helper.top", "img.t.sinajs.cn"]
    }
  };

  // config.mysql = {
  //   // 单数据库信息配置
  //   client: {
  //     host: 'localhost',
  //     port: '3306',
  //     user: 'root',
  //     password: 'CAIqi211',
  //     database: 'shop',
  //   },
  //   app: true,
  //   agent: false,
  // };
  config.jwt = {
    web: 'web-asdf123453',
    admin: 'admin-asdf12345',
    enable: true, // default is false
    match: '/jwt', // optional
  };

  config.sequelize = {
    dialect: 'mysql',
    host: 'localhost',
    port: 3306,
    database: 'shop',
    username: 'root',
    password: 'CAIqi211',
    timezone: '+08:00', // 保存为本地时区
    define: {
      underscored: true, // 注意需要加上这个， egg-sequelize只是简单的使用Object.assign对配置和默认配置做了merge, 如果不加这个 update_at会被转变成 updateAt故报错
      // 禁止修改表名，默认情况下，sequelize将自动将所有传递的模型名称（define的第一个参数）转换为复数
      // 但是为了安全着想，复数的转换可能会发生变化，所以禁止该行为
      freezeTableName: true
    },
    dialectOptions: {
      dateStrings: true,
      typeCast(field, next) {
        if (field.type === "DATETIME") {
          return field.string();
        }
        return next();
      }
    }
  }

  // 七牛云配置
  config.qiniu = {
    ACCESS_KEY: 'E4lMsWNebe8_XhTUvn4qVvjqeV7RdWwvUVexh3Gx',
    SECRET_KEY: '4137YbY9JSc7GiuJIKuk0JgFOr05b1y97WsrmJT6',
    name: 'helper',
    domain: 'http://cdn.web-helper.top',
  };

  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`
  };

  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig
  };
};
